﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Main_Utilities_SetEmailProperty : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            if (Request.QueryString["action"] != null)
            {
                if (Request.QueryString["action"].ToString() == "view")
                {
                    GetData();
                    pnlview.Visible = true;
                    pnlinsert.Visible = false;
                }
            }
            else
            {
                pnlinsert.Visible = true;
                pnlview.Visible = false;
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            SqlParameter[] param = {new SqlParameter("@MailUserId",txtmailuserid.Text),
                                   new SqlParameter("@MailPassword",txtmailpassword.Text),
                                   new SqlParameter("@MailFrom",txtmailfrom.Text),
                                   new SqlParameter("@MailDisplayName",txtmaildisplayname.Text),
                                   new SqlParameter("@MailHost",txtmailhost.Text),
                                   new SqlParameter("@MailPort",txtmailport.Text),                                   
                                   new SqlParameter("@Cname",Session["username"].ToString())
                                   };
            int insert = DU.executeprocedure("Acc_Save_EmailSetting", param);
            if (insert > 0)
            {
                //GetData();
                ClearControl();
                lblmsg.Visible = true;
                lblmsg.Text = "Record Save SuccessFully.";
                lblmsg.ForeColor = System.Drawing.Color.Green;
                lblmsg.Font.Bold = true;
            }
        }
        catch (Exception ex)
        {
        }
    }
    protected void GetData()
    {
        try
        {
            DataTable Dt = DU.getdata("Select * from Acc_EmailSetting order by id desc");
            if (Dt.Rows.Count > 0)
            {
                grdview.DataSource = Dt;
                grdview.DataBind();
            }
        }
        catch (Exception ex)
        {

        }
    }
    protected void ClearControl()
    {
        txtmaildisplayname.Text = "";
        txtmailfrom.Text = "";
        txtmailhost.Text = "";
        txtmailpassword.Text = "";
        txtmailport.Text = "";
        txtmailuserid.Text = "";        
    }
    protected void grdview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdview.EditIndex = -1;
        GetData();
    }
    protected void grdview_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            string ID = grdview.DataKeys[e.RowIndex].Value.ToString();
            //findcontrol
            TextBox txtUpdatemailUserId = (TextBox)grdview.Rows[e.RowIndex].FindControl("txtMailUserid");
            TextBox txtUpdateMailFrom = (TextBox)grdview.Rows[e.RowIndex].FindControl("txtMailFrom");
            TextBox txtUpdatedisplayname = (TextBox)grdview.Rows[e.RowIndex].FindControl("txtMaildisplayName");
            TextBox txtUpdatehost = (TextBox)grdview.Rows[e.RowIndex].FindControl("txtMailHost");
            TextBox txtUpdateport = (TextBox)grdview.Rows[e.RowIndex].FindControl("txtMailPort");

            SqlParameter[] ParamUpdate = {new SqlParameter("@mailuserid",txtUpdatemailUserId.Text),
                                      new SqlParameter("@mailfrom",txtUpdateMailFrom.Text),
                                      new SqlParameter("@maildisplayname",txtUpdatedisplayname.Text),
                                      new SqlParameter("@mailhost",txtUpdatehost.Text),
                                      new SqlParameter("@mailport",txtUpdateport.Text),
                                      new SqlParameter("@Id",ID),
                                      new SqlParameter("@ename",Session["username"].ToString())
                                     };
            int insert = DU.executeprocedure("ACC_Update_EmailSetting", ParamUpdate);
            if (insert > 0)
            {
                grdview.EditIndex = -1;
                GetData();
            }
        }
        catch (Exception ex)
        {
        }
    }
    protected void grdview_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdview.EditIndex = e.NewEditIndex;
        GetData();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ClearControl();
        lblmsg.Visible = false;
    }
    protected void grdview_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string ID = grdview.DataKeys[e.RowIndex].Value.ToString();
        string SqlDelete = "Delete From Acc_EmailSetting where Id='" + ID + "'";
        int result = DU.executesql(SqlDelete);
        if (result > 0)
        {
            lblmsg.Visible = true;
            lblmsg.Text = "Record Deleted SuccessFully.";
            lblmsg.ForeColor = System.Drawing.Color.Green;
            lblmsg.Font.Bold = true;
            GetData();
        }
    }
}